class Spirit {
    constructor(game, config) {
        this.game = game;
        // this.map = game.map;

        //{type: 1, weight: 10, speed: 7, blood: 50, fire_calibrate: 50, fire_scale: 2},
        this.config = config;
        this.blood = config.blood;

        this.cur_map_cell = this.game.map.map_vector[0];
        this.next_map_cell = this.cur_map_cell.last_map_cell;

        this.position = this.cur_map_cell.get_center_xy();
        this.next_pos = this.next_map_cell && this.next_map_cell.get_center_xy();
        this.move_direct = 1; // 0:up, 1:right, 2:down, 3:left
    }

    move_on() {
        if (!this.blood) return;

        let ts = new Date().getTime();
        if (this.last_ts && ts - this.last_ts < this.config.move_interval) {
            return;
        }

        if (!this.next_pos) {
            this.next_map_cell = this.cur_map_cell.last_map_cell;
            this.next_pos = this.next_map_cell && this.next_map_cell.get_center_xy();
        }
        if (!(this.position && this.next_pos)) return;

        let [x1, y1] = this.position;
        let [x2, y2] = this.next_pos;

        if (x1 === x2 && y1 === y2) {
            this.cur_map_cell = this.next_map_cell;
            let next_map_cell = this.next_map_cell.last_map_cell;
            if (!next_map_cell) {
                let {col, row} = this.cur_map_cell;
                let map_size = this.game.map.map_size - 1;
                if (row === map_size && col === map_size) {
                    this.game.game_info.escape_spirit_count++;
                    this.blood = 0;
                    this.lost = true;
                }
                return;
            }

            this.next_map_cell = next_map_cell;
            this.next_pos = next_map_cell.get_center_xy();

            this.last_ts = ts;
        }
        this.move_step();
    };

    move_step() {
        let [x1, y1] = this.position;
        let [x2, y2] = this.next_pos;
        let speed = this.config.speed;

        if (x1 < x2) {
            x1 += speed;
            x1 = x1 > x2 ? x2 : x1;
            this.move_direct = 1;
        }
        if (x1 > x2) {
            x1 -= speed;
            x1 = x1 < x2 ? x2 : x1;
            this.move_direct = 3;
        }
        if (y1 < y2) {
            y1 += speed;
            y1 = y1 > y2 ? y2 : y1;
            this.move_direct = 2;
        }
        if (y1 > y2) {
            y1 -= speed;
            y1 = y1 < y2 ? y2 : y1;
            this.move_direct = 0;
        }

        this.position = [x1, y1]
    };

    draw() {
        this.draw_step();
    };

    draw_step() {
        if (!this.blood) return;

        let ctx = this.game.stage_canvas_ctx;

        let [x, y] = this.position;
        let r = this.config.weight;

        ctx.fillStyle = "rgba(50, 50, 50," + (this.blood / this.config.blood) + ")";
        ctx.beginPath();
        ctx.arc(x, y, r, 0, Math.PI * 2, true);
        ctx.fill();

        ctx.lineWidth = this.config.weight / 4 | 0;
        ctx.setLineDash([this.config.weight, this.config.weight]);

        ctx.strokeStyle = "#333";
        ctx.arc(x, y, r, 0, Math.PI * 2, true);
        ctx.stroke();
        ctx.setLineDash([]);

        ctx.textAlign = 'center';
        ctx.font = "15px yahei";
        ctx.fillStyle = "#fff";
        ctx.fillText(this.blood, x, y + 5);
    };
}

export default Spirit;